#include "metestationform.h"
#include "ui_metestationform.h"

#include <QSplineSeries>
#include <QValueAxis>
#include <QDateTimeAxis>
#include <QDateTime>
#include <QLegendMarker>

#include "../deviceClass/metestation.h"


MeteStationForm::MeteStationForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MeteStationForm)
{
    ui->setupUi(this);

    this->InitChart_Temperature();
    this->InitChart_Humidity();
    this->InitChart_Pressure();
    this->InitChart_Rain();
    this->InitChart_WindSpeed();
    this->InitChart_WindDirection();

    connect(MeteStation::getInstance(), &MeteStation::updateView, this, [=, this](const MeteStationProtocolAll& protocol){
        ui->label_temperature->setText(QString::number(protocol.temperature));
        ui->label_humidity->setText(QString::number(protocol.humidity));
        ui->label_pressure->setText(QString::number(protocol.pressure));
        ui->label_minute_precipitation->setText(QString::number(protocol.minute_precipitation));
        ui->label_hour_precipitation->setText(QString::number(protocol.hour_precipitation));
        ui->label_one_minute_maxWindSpeed->setText(QString::number(protocol.one_minute_maxWindSpeed));
        ui->label_one_minute_maxWindDirection->setText(QString::number(protocol.one_minute_windDirection));
        ui->label_two_minute_avgWindSpeed->setText(QString::number(protocol.two_minute_avgWindSpeed));
        ui->label_two_minute_avgWindDirection->setText(QString::number(protocol.two_minute_avgWindDirection));
        ui->label_ten_minute_avgWindSpeed->setText(QString::number(protocol.ten_minute_avgWindSpeed));
        ui->label_ten_minute_avgWindDirection->setText(QString::number(protocol.ten_minute_avgWindDirection));


    });
}

MeteStationForm::~MeteStationForm()
{
    delete ui;
}


void MeteStationForm::InitChart_Temperature()
{
    m_chart_Temperature = new QChart();

    QSplineSeries* m_series_temperature = new QSplineSeries(m_chart_Temperature);
    m_series_temperature->setPen(QPen(QBrush(Qt::blue),2, Qt::PenStyle::SolidLine,
                                      Qt::PenCapStyle::RoundCap, Qt::PenJoinStyle::RoundJoin));


    m_chart_Temperature->addSeries(m_series_temperature);


    // 更改标题颜色和字体大小
    QFont titleFont("sans-serif", 12, QFont::Bold);
    m_chart_Temperature->setTitleFont(titleFont);
    QBrush titleBrush(Qt::black);
    m_chart_Temperature->setTitleBrush(titleBrush);

    // 设置y轴
    QValueAxis *axisY_tem = new QValueAxis();
    axisY_tem->setRange(-100, 100);
    axisY_tem->setTickCount(6);
    axisY_tem->setLabelFormat("%d");


    // 设置x轴
    QDateTimeAxis* axisX = new QDateTimeAxis;
    axisX->setTickCount(4);
    axisX->setFormat("hh:mm");
    axisX->setRange(QDateTime::currentDateTime().addSecs(-60), QDateTime::currentDateTime().addMSecs(500));

    m_chart_Temperature->addAxis(axisX, Qt::AlignBottom);
    m_chart_Temperature->addAxis(axisY_tem, Qt::AlignmentFlag::AlignLeft);


    m_series_temperature->attachAxis(axisX);
    m_series_temperature->attachAxis(axisY_tem);


    m_chart_Temperature->legend()->markers().at(0)->setVisible(true);
    m_chart_Temperature->legend()->markers().at(0)->setFont(QFont("微软雅黑",12));
    m_chart_Temperature->legend()->markers().at(0)->setPen(QPen(Qt::blue));
    m_series_temperature->setName("温度℃");


    m_chart_Temperature->legend()->attachToChart();

    ui->graphicsView_temperature->setChart(m_chart_Temperature);
    ui->graphicsView_temperature->setRenderHint(QPainter::Antialiasing);

    connect(MeteStation::getInstance(), &MeteStation::updateView, this, [=](const MeteStationProtocolAll& point){
        qDebug()<<"point.time:"<<point.time.toString();
        m_series_temperature->append(QDateTime(point.date, point.time).toMSecsSinceEpoch(), point.temperature);
        if(m_series_temperature->count() > 100){
            m_series_temperature->removePoints(0, m_series_temperature->count() - 100);
        }
        QDateTime start1 = QDateTime(point.date, point.time).addSecs(-3600);
        QDateTime end1 = QDateTime(point.date, point.time).addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_temperature->update();
    });


    /*
    connect(this, &MeteStationForm::updateView_temperature, this, [&](int temperature){

        m_series_temperature->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), temperature);
        if(m_series_temperature->count() > 100){
            m_series_temperature->removePoints(0, m_series_temperature->count() - 100);
        }


        QDateTime start1 = axisX->min().addSecs(500);
        QDateTime end1 = axisX->max().addSecs(10000);
        axisX->setRange(start1, end1);
        ui->graphicsView_temperature->update();
    });
    */
}

void MeteStationForm::InitChart_Humidity(){
    m_yAxis_humidity_Max.append(INT_MIN);
    m_yAxis_humidity_Min.append(INT_MAX);

    m_chart_Humidity = new QChart();

    QSplineSeries* m_series_humidity = new QSplineSeries(m_chart_Humidity);
    m_series_humidity->setPen(QPen(QBrush(Qt::green),2, Qt::PenStyle::SolidLine,
                                   Qt::PenCapStyle::RoundCap, Qt::PenJoinStyle::RoundJoin));

    m_chart_Humidity->addSeries(m_series_humidity);


    // 更改标题颜色和字体大小
    QFont titleFont("sans-serif", 12, QFont::Bold);
    m_chart_Humidity->setTitleFont(titleFont);
    QBrush titleBrush(Qt::black);
    m_chart_Humidity->setTitleBrush(titleBrush);

    // 设置y轴
    QValueAxis *axisY = new QValueAxis();
    axisY->setRange(0, 3000);
    axisY->setTickCount(10);
    axisY->setLabelFormat("%d");

    // 设置x轴
    QDateTimeAxis* axisX = new QDateTimeAxis;
    axisX->setTickCount(4);
    axisX->setFormat("hh:mm");
    axisX->setRange(QDateTime::currentDateTime().addSecs(-60), QDateTime::currentDateTime().addMSecs(500));

    m_chart_Humidity->addAxis(axisX, Qt::AlignBottom);
    m_chart_Humidity->addAxis(axisY, Qt::AlignmentFlag::AlignLeft);

    m_series_humidity->attachAxis(axisX);
    m_series_humidity->attachAxis(axisY);

    m_chart_Humidity->legend()->markers().at(0)->setVisible(true);
    m_chart_Humidity->legend()->markers().at(0)->setFont(QFont("微软雅黑",12));
    m_chart_Humidity->legend()->markers().at(0)->setPen(QPen(Qt::blue));
    m_series_humidity->setName("湿度%RH");


    m_chart_Humidity->legend()->attachToChart();

    ui->graphicsView_humidity->setChart(m_chart_Humidity);
    ui->graphicsView_humidity->setRenderHint(QPainter::Antialiasing);

    connect(MeteStation::getInstance(), &MeteStation::updateView, this, [=](const MeteStationProtocolAll& point){
        qDebug()<<"更新湿度表";
        m_series_humidity->append(QDateTime(point.date, point.time).toMSecsSinceEpoch(), point.humidity);
        //入队
        if(!this->m_yAxis_humidity_Max.empty() && point.humidity > this->m_yAxis_humidity_Max[0]){
            this->m_yAxis_humidity_Max.clear();
        }
        else if(!this->m_yAxis_humidity_Max.empty() && point.humidity <= this->m_yAxis_humidity_Max[0]){
            //删除数组中小于point.humidity的值
            for(int i = 1; i < this->m_yAxis_humidity_Max.size();){
                if(this->m_yAxis_humidity_Max[i] < point.humidity){
                    this->m_yAxis_humidity_Max.remove(i);
                }else{
                    ++i;
                }
            }
        }
        this->m_yAxis_humidity_Max.push_back(point.humidity);

        if(!this->m_yAxis_humidity_Min.empty() && point.humidity > this->m_yAxis_humidity_Min[0]){
            this->m_yAxis_humidity_Min.clear();
        }
        else if(!this->m_yAxis_humidity_Min.empty() && point.humidity <= this->m_yAxis_humidity_Min[0]){
            //删除数组中小于point.humidity的值
            for(int i = 1; i < this->m_yAxis_humidity_Min.size();){
                if(this->m_yAxis_humidity_Min[i] < point.humidity){
                    this->m_yAxis_humidity_Min.remove(i);
                }else{
                    ++i;
                }
            }
        }
        this->m_yAxis_humidity_Min.push_back(point.humidity);
        //出队
        if(m_series_humidity->count() > 100){
            if(m_series_humidity->at(0).y() == this->m_yAxis_humidity_Max[0]){
                this->m_yAxis_humidity_Max.pop_front();
            }
            if(m_series_humidity->at(0).y() == this->m_yAxis_humidity_Min[0]){
                this->m_yAxis_humidity_Min.pop_front();
            }
            m_series_humidity->removePoints(0, m_series_humidity->count() - 100);
        }

        QDateTime start = QDateTime(point.date, point.time).addSecs(-3600);
        QDateTime end = QDateTime(point.date, point.time).addMSecs(500);
        axisX->setRange(start, end);
        axisY->setRange(this->m_yAxis_humidity_Min[0], this->m_yAxis_humidity_Max[0]);
        ui->graphicsView_humidity->update();
    });

    /*connect(this, &MeteStationForm::updateView_humidity, this, [&](int humidity){

        m_series_humidity->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), humidity);
        if(m_series_humidity->count() > 100){
            m_series_humidity->removePoints(0, m_series_humidity->count() - 100);
        }

        QDateTime start1 = axisX->min().addMSecs(1000);
        QDateTime end1 = axisX->max().addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_pressure->update();
    });*/
}

void MeteStationForm::InitChart_Pressure()
{
    m_chart_Pressure = new QChart();

    QSplineSeries* m_series_pressure = new QSplineSeries(m_chart_Pressure);
    m_series_pressure->setPen(QPen(QBrush(Qt::yellow),2, Qt::PenStyle::SolidLine,
                                   Qt::PenCapStyle::RoundCap, Qt::PenJoinStyle::RoundJoin));

    m_chart_Pressure->addSeries(m_series_pressure);


    // 更改标题颜色和字体大小
    QFont titleFont("sans-serif", 12, QFont::Bold);
    m_chart_Pressure->setTitleFont(titleFont);
    QBrush titleBrush(Qt::black);
    m_chart_Pressure->setTitleBrush(titleBrush);

    // 设置y轴
    QValueAxis *axisY = new QValueAxis();
    axisY->setRange(0, 3000);
    axisY->setTickCount(10);
    axisY->setLabelFormat("%d");

    // 设置x轴
    QDateTimeAxis* axisX = new QDateTimeAxis;
    axisX->setTickCount(4);
    axisX->setFormat("hh:mm");
    axisX->setRange(QDateTime::currentDateTime().addSecs(-60), QDateTime::currentDateTime().addMSecs(500));

    m_chart_Pressure->addAxis(axisX, Qt::AlignBottom);
    m_chart_Pressure->addAxis(axisY, Qt::AlignmentFlag::AlignLeft);

    m_series_pressure->attachAxis(axisX);
    m_series_pressure->attachAxis(axisY);

    m_chart_Pressure->legend()->markers().at(0)->setVisible(true);
    m_chart_Pressure->legend()->markers().at(0)->setFont(QFont("微软雅黑",12));
    m_chart_Pressure->legend()->markers().at(0)->setPen(QPen(Qt::blue));
    m_series_pressure->setName("气压hPa");


    m_chart_Pressure->legend()->attachToChart();

    ui->graphicsView_pressure->setChart(m_chart_Pressure);
    ui->graphicsView_pressure->setRenderHint(QPainter::Antialiasing);

    connect(MeteStation::getInstance(), &MeteStation::updateView, this, [=](const MeteStationProtocolAll& point){
        m_series_pressure->append(QDateTime(point.date, point.time).toMSecsSinceEpoch(), point.pressure);
        if(m_series_pressure->count() > 100){
            m_series_pressure->removePoints(0, m_series_pressure->count() - 100);
        }
        QDateTime start1 = QDateTime(point.date, point.time).addSecs(-3600);
        QDateTime end1 = QDateTime(point.date, point.time).addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_pressure->update();
    });

    /*connect(this, &MeteStationForm::updateView_pressure, this, [&](int pressure){

        m_series_pressure->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), pressure);
        if(m_series_pressure->count() > 100){
            m_series_pressure->removePoints(0, m_series_pressure->count() - 100);
        }

        QDateTime start1 = axisX->min().addMSecs(1000);
        QDateTime end1 = axisX->max().addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_pressure->update();
    });*/
}

void MeteStationForm::InitChart_Rain()
{
    m_chart_Rain = new QChart();

    QSplineSeries* m_series_rain = new QSplineSeries(m_chart_Rain);
    m_series_rain->setPen(QPen(QBrush(Qt::cyan),2, Qt::PenStyle::SolidLine,
                               Qt::PenCapStyle::RoundCap, Qt::PenJoinStyle::RoundJoin));

    m_chart_Rain->addSeries(m_series_rain);


    // 更改标题颜色和字体大小
    QFont titleFont("sans-serif", 12, QFont::Bold);
    m_chart_Rain->setTitleFont(titleFont);
    QBrush titleBrush(Qt::black);
    m_chart_Rain->setTitleBrush(titleBrush);

    // 设置y轴
    QValueAxis *axisY = new QValueAxis();
    axisY->setRange(0, 500);
    axisY->setTickCount(10);
    axisY->setLabelFormat("%d");

    // 设置x轴
    QDateTimeAxis* axisX = new QDateTimeAxis;
    axisX->setTickCount(4);
    axisX->setFormat("hh:mm");
    axisX->setRange(QDateTime::currentDateTime().addSecs(-60), QDateTime::currentDateTime().addMSecs(500));

    m_chart_Rain->addAxis(axisX, Qt::AlignBottom);
    m_chart_Rain->addAxis(axisY, Qt::AlignmentFlag::AlignLeft);

    m_series_rain->attachAxis(axisX);
    m_series_rain->attachAxis(axisY);

    m_chart_Rain->legend()->markers().at(0)->setVisible(true);
    m_chart_Rain->legend()->markers().at(0)->setFont(QFont("微软雅黑",12));
    m_chart_Rain->legend()->markers().at(0)->setPen(QPen(Qt::blue));
    m_series_rain->setName("雨量mm");


    m_chart_Rain->legend()->attachToChart();

    ui->graphicsView_rain->setChart(m_chart_Rain);
    ui->graphicsView_rain->setRenderHint(QPainter::Antialiasing);

    connect(MeteStation::getInstance(), &MeteStation::updateView, this, [=](const MeteStationProtocolAll& point){
        qDebug()<<"point.time:"<<point.time.toString();
        m_series_rain->append(QDateTime(point.date, point.time).toMSecsSinceEpoch(), point.minute_precipitation);
        if(m_series_rain->count() > 100){
            m_series_rain->removePoints(0, m_series_rain->count() - 100);
        }
        QDateTime start1 = QDateTime(point.date, point.time).addSecs(-3600);
        QDateTime end1 = QDateTime(point.date, point.time).addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_rain->update();
    });

    /*connect(this, &MeteStationForm::updateView_rain, this, [&](int rain){

        m_series_rain->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), rain);
        if(m_series_rain->count() > 100){
            m_series_rain->removePoints(0, m_series_rain->count() - 100);
        }

        QDateTime start1 = axisX->min().addMSecs(1000);
        QDateTime end1 = axisX->max().addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_rain->update();
    });*/
}

void MeteStationForm::InitChart_WindSpeed()
{
    m_chart_WindSpeed = new QChart();

    QSplineSeries* m_series_wind = new QSplineSeries(m_chart_WindSpeed);
    m_series_wind->setPen(QPen(QBrush(Qt::magenta),2, Qt::PenStyle::SolidLine,
                               Qt::PenCapStyle::RoundCap, Qt::PenJoinStyle::RoundJoin));

    m_chart_WindSpeed->addSeries(m_series_wind);


    // 更改标题颜色和字体大小
    QFont titleFont("sans-serif", 12, QFont::Bold);
    m_chart_WindSpeed->setTitleFont(titleFont);
    QBrush titleBrush(Qt::black);
    m_chart_WindSpeed->setTitleBrush(titleBrush);

    // 设置y轴
    QValueAxis *axisY = new QValueAxis();
    axisY->setRange(0, 100);
    axisY->setTickCount(10);
    axisY->setLabelFormat("%d");

    // 设置x轴
    QDateTimeAxis* axisX = new QDateTimeAxis;
    axisX->setTickCount(4);
    axisX->setFormat("hh:mm");
    axisX->setRange(QDateTime::currentDateTime().addSecs(-60), QDateTime::currentDateTime().addMSecs(500));

    m_chart_WindSpeed->addAxis(axisX, Qt::AlignBottom);
    m_chart_WindSpeed->addAxis(axisY, Qt::AlignmentFlag::AlignLeft);

    m_series_wind->attachAxis(axisX);
    m_series_wind->attachAxis(axisY);

    m_chart_WindSpeed->legend()->markers().at(0)->setVisible(true);
    m_chart_WindSpeed->legend()->markers().at(0)->setFont(QFont("微软雅黑",12));
    m_chart_WindSpeed->legend()->markers().at(0)->setPen(QPen(Qt::blue));
    m_series_wind->setName("风速m/s");


    m_chart_WindSpeed->legend()->attachToChart();

    ui->graphicsView_windvelocity->setChart(m_chart_WindSpeed);
    ui->graphicsView_windvelocity->setRenderHint(QPainter::Antialiasing);

    connect(MeteStation::getInstance(), &MeteStation::updateView, this, [=](const MeteStationProtocolAll& point){
        qDebug()<<"point.time:"<<point.time.toString();
        m_series_wind->append(QDateTime(point.date, point.time).toMSecsSinceEpoch(), point.one_minute_maxWindSpeed);
        if(m_series_wind->count() > 100){
            m_series_wind->removePoints(0, m_series_wind->count() - 100);
        }
        QDateTime start1 = QDateTime(point.date, point.time).addSecs(-3600);
        QDateTime end1 = QDateTime(point.date, point.time).addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_windvelocity->update();
    });


    /*connect(this, &MeteStationForm::updateView_wind, this, [&](int wind){

        m_series_wind->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), wind);
        if(m_series_wind->count() > 100){
            m_series_wind->removePoints(0, m_series_wind->count() - 100);
        }

        QDateTime start1 = axisX->min().addMSecs(1000);
        QDateTime end1 = axisX->max().addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_windvelocity->update();
    });*/


}

void MeteStationForm::InitChart_WindDirection(){
    m_chart_WindDirection = new QChart();

    QSplineSeries* m_series_windDirection = new QSplineSeries(m_chart_WindDirection);
    m_series_windDirection->setPen(QPen(QBrush(Qt::gray),2, Qt::PenStyle::SolidLine,
                                        Qt::PenCapStyle::RoundCap, Qt::PenJoinStyle::RoundJoin));

    m_chart_WindDirection->addSeries(m_series_windDirection);


    // 更改标题颜色和字体大小
    QFont titleFont("sans-serif", 12, QFont::Bold);
    m_chart_WindDirection->setTitleFont(titleFont);
    QBrush titleBrush(Qt::black);
    m_chart_WindDirection->setTitleBrush(titleBrush);

    // 设置y轴
    QValueAxis *axisY = new QValueAxis();
    axisY->setRange(0, 360);
    axisY->setTickCount(10);
    axisY->setLabelFormat("%d");

    // 设置x轴
    QDateTimeAxis* axisX = new QDateTimeAxis;
    axisX->setTickCount(4);
    axisX->setFormat("hh:mm");
    axisX->setRange(QDateTime::currentDateTime().addSecs(-60), QDateTime::currentDateTime().addMSecs(500));

    m_chart_WindDirection->addAxis(axisX, Qt::AlignBottom);
    m_chart_WindDirection->addAxis(axisY, Qt::AlignmentFlag::AlignLeft);

    m_series_windDirection->attachAxis(axisX);
    m_series_windDirection->attachAxis(axisY);

    m_chart_WindDirection->legend()->markers().at(0)->setVisible(true);
    m_chart_WindDirection->legend()->markers().at(0)->setFont(QFont("微软雅黑",12));
    m_chart_WindDirection->legend()->markers().at(0)->setPen(QPen(Qt::blue));
    m_series_windDirection->setName("风向°");


    m_chart_WindDirection->legend()->attachToChart();

    ui->graphicsView_windDirection->setChart(m_chart_WindDirection);
    ui->graphicsView_windDirection->setRenderHint(QPainter::Antialiasing);


    connect(MeteStation::getInstance(), &MeteStation::updateView, this, [=](const MeteStationProtocolAll& point){
        qDebug()<<"point.time:"<<point.time.toString();
        m_series_windDirection->append(QDateTime(point.date, point.time).toMSecsSinceEpoch(), point.one_minute_windDirection);
        if(m_series_windDirection->count() > 100){
            m_series_windDirection->removePoints(0, m_series_windDirection->count() - 100);
        }
        QDateTime start1 = QDateTime(point.date, point.time).addSecs(-3600);
        QDateTime end1 = QDateTime(point.date, point.time).addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_windDirection->update();
    });

   /* connect(this, &MeteStationForm::updateView_wind, this, [&](int wind){

        m_series_windDirection->append(QDateTime::currentDateTime().toMSecsSinceEpoch(), wind);
        if(m_series_windDirection->count() > 100){
            m_series_windDirection->removePoints(0, m_series_windDirection->count() - 100);
        }

        QDateTime start1 = axisX->min().addMSecs(1000);
        QDateTime end1 = axisX->max().addMSecs(1000);
        axisX->setRange(start1, end1);
        ui->graphicsView_windDirection->update();
    });*/

}

